﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace TaoXML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XDocument doc= new XDocument(
                new XComment("this is the comment"),
                new XElement("root",
                    new XElement("info1","info2"),
                    new XElement("info3","info3")
                    )
            );
            doc.Save("root.xml");
          XDocument xml = XDocument.Load("root.xml");
          textBox1.Text = xml.ToString();
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            XDocument xmdoc = XDocument.Load("BOOKS.xml");
            textBox1.Text = xmdoc.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            XElement doc = XElement.Load("BOOKS.xml");
            IEnumerable<XElement> category = from el in doc.Elements("book")
                                             select el;
            foreach (XElement v in category)
            {
                textBox1.Text += (string)v.Attribute("category") + "\r\n";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {

            XElement doc = XElement.Load("BOOKS.xml");
            IEnumerable<XElement> catagory = from el in doc.Elements("book")
                                             select el;
            foreach (XElement v in catagory)
            {
                textBox1.Text = (string)v.Element("title") +"\r\n"+ (string)v.Element("author");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            XElement doc = XElement.Load("BOOKS.xml");
            IEnumerable<XElement> catagory = from el in doc.Elements("book")
                                             select el;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            XElement doc = XElement.Load("BOOKS.xml");
            IEnumerable<XElement> category = from el in doc.Elements("book")
                                             where (string)el.Attribute("category")=="cooking"
                                             select el;
            foreach (XElement v in category)
            {
                doc.Elements().Attributes().Remove();
                textBox1.Text = doc.ToString();
               // textBox1.Text += (string)v. + "\r\n";
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            XElement doc = XElement.Load("BOOKS.xml");
            doc.Element("book").AddAfterSelf(
                new XElement("book catagory=", "programming",
                    new XElement("title lang=vi ", "advanced Programming C#"),
                    new XElement("author ", "Dentis")));
            doc.Save("BOOK.xml");
            XDocument xml = XDocument.Load("BOOK.xml");
            textBox1.Text = xml.ToString();
        }
    }

}
